package com.biocar.controller;

import com.biocar.ResBean;
import com.biocar.bean.Medicine;
import com.biocar.enums.PageSplitSizeLimiter;
import com.biocar.response.PageResponse;
import com.biocar.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @menu 药品接口
 * @author DeSen Xu
 * @date 2021-11-22 22:34
 */
@RestController
@RequestMapping(value = "/medicine", produces = MediaType.APPLICATION_JSON_VALUE)
public class MedicineController {


    private MedicineService medicineService;

    @Autowired
    public void setMedicineService(MedicineService medicineService) {
        this.medicineService = medicineService;
    }

    /**
     * 获取药品列表
     * @status done
     * @param index 显示第几页,最小为1
     * @param max 每页最多显示多少行
     * @param sortOrder 排序指令, 0(default): 按照创建时间降序排序 1: 按照查看量降序排序 其它: id降序
     * @return 药品列表,返回示例:
     * {
     *     "code": 20000,
     *     "message": "success",
     *     "data": {
     *         "totalCount": 10,
     *         "totalPages": 1,
     *         "currentPage": 1,
     *         "list": [
     *             {
     *                 "id": 757,
     *                 "name": "利托那韦",
     *                 "countSize": "56",
     *                 "createdAt": "1637424000000"
     *             },
     *             {
     *                 "id": 756,
     *                 "name": "拉帕替尼",
     *                 "countSize": "24",
     *                 "createdAt": "1637424000000"
     *             }
     *         ]
     *     }
     * }
     */
    @GetMapping("/list")
    public ResBean<PageResponse<Medicine>> getMedicines(@RequestParam Integer index,
                                                        @RequestParam(required = false, defaultValue = "10") Integer max,
                                                        @RequestParam(required = false, defaultValue = "0") Integer sortOrder) {
        if (PageSplitSizeLimiter.SEARCH_SIZE_LIMIT.isUnsatisfied(max)) {
            return ResBean.failWithMsg("每页的显示量不符合要求");
        }
        if (PageSplitSizeLimiter.SEARCH_INDEX_LIMIT.isUnsatisfied(index)) {
            return ResBean.failWithMsg("给定的索引值有误");
        }
        PageResponse<Medicine> medicines = medicineService.queryMedicine(index, max, sortOrder);
        return ResBean.successWithObj(medicines);
    }
}
